#include "rbl_algo.h"
#if FUPDATE_USING_AES256

#include <tinycrypt.h>
#include <string.h>

typedef struct rbl_aes_context
{
	tiny_aes_context aes_context;
	
	uint8_t iv_buffer[18];
}rbl_aes_context_t;

void *rbl_aes256_decrypt_context_create(void)
{
	rbl_aes_context_t * rbl_aes_context  = rt_calloc(1, sizeof(rbl_aes_context_t));
	return rbl_aes_context;
}
void rbl_aes256_decrypt_context_delete(void *rbl_aes_ctx)
{
	rt_free(rbl_aes_ctx);
}

void rbl_aes256_decrypt_init_keyiv(void * rbl_aes_ctx,const uint8_t *aes_key, const uint8_t *aes_iv)
{
	rbl_aes_context_t *rbl_aes_context  = rbl_aes_ctx;
	
	int len = rt_strlen((const char*)aes_iv);
	if(len > 16)
	{
		len = 16;
	}
	rt_memset(rbl_aes_context->iv_buffer, 0, 18);
  rt_memcpy(rbl_aes_context->iv_buffer, aes_iv, len);
	tiny_aes_setkey_dec(&(rbl_aes_context->aes_context),(uint8_t *) aes_key, 256);
}

void rbl_aes256_decrypt_data(void * rbl_aes_ctx, uint8_t *src_buf, uint8_t *dst_buf,  uint32_t len)
{
	rbl_aes_context_t *rbl_aes_context  = rbl_aes_ctx;
	tiny_aes_crypt_cbc(&(rbl_aes_context->aes_context), AES_DECRYPT, len, rbl_aes_context->iv_buffer, src_buf, dst_buf);
}

#endif
